﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Lcygcc.Application.WCF.Client
{
    public class WCFClientHost
    {
        private static WCFClientHost m_WCFClientHost;
        private static IFactoryLayoutWCFService m_Channel;
        private WCFClientHost()
        {

        }
        public static WCFClientHost Instance(string url)
        {
            WCFClientHost result;
            if (WCFClientHost.StartWCFClient(url))
            {
                WCFClientHost.m_WCFClientHost = new WCFClientHost();
                result = WCFClientHost.m_WCFClientHost;
            }
            else
            {
                result = null;
            }
            return result;

        }
        private static bool StartWCFClient(string url)
        {
            bool result = false;
            try
            {
                ChannelFactory<IFactoryLayoutWCFService> channelFactory = new ChannelFactory<IFactoryLayoutWCFService>(new BasicHttpBinding
                {
                    MaxReceivedMessageSize = 2147483647L,
                    TransferMode = TransferMode.Streamed,
                    MaxBufferSize = 2147483647,
                    MaxBufferPoolSize = 2147483647L,
                    ReaderQuotas = new XmlDictionaryReaderQuotas
                    {
                        MaxStringContentLength = 2147483647
                    },
                    SendTimeout = new TimeSpan(0, 0, 8)
                });
                EndpointAddress address = new EndpointAddress(url);
                WCFClientHost.m_Channel = channelFactory.CreateChannel(address);
                if (WCFClientHost.m_Channel != null)
                {
                    Console.WriteLine("GCB_WCF连接成功！");
                    result = true;
                }
                else
                {
                    result = false;
                }
            }
            catch (Exception arg)
            {
                Console.WriteLine("GCB_WCF连接失败：" + arg);
                result = false;
            }
            return result;
        }
        public string GetAllPlant()
        {
            string resultStr = "";
            try
            {
                resultStr = WCFClientHost.m_Channel.GetAllPlant();
            }
            catch (Exception arg)
            {
                Console.WriteLine("GCB_WCF发送失败：" + arg);
            }
            return resultStr;
        }
        public string OpenGateByCredentialNO(string macNO, string parkID, string credentialNO)
        {
            try
            {
                return WCFClientHost.m_Channel.GetAllPlant();
            }
            catch (Exception arg)
            {
                Console.WriteLine("GCB_WCF发送失败：" + arg);
            }
            return null;
        }
    }
}
